package cn.gogpay.duoim.sdk.service;

import cn.gogpay.duoim.sdk.constant.IMApis;
import cn.gogpay.duoim.sdk.constant.IMRespCode;
import cn.gogpay.duoim.sdk.exception.IMSDKException;
import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.ManagementSendMsgReq;
import cn.gogpay.duoim.sdk.model.UserSendMsgResp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import okhttp3.Request;

/**
 * 消息
 */
public class IMMessageService {

    /**
     * 管理员发送消息
     *
     * @param req 请求
     * @return 响应
     */
    public IMRespResult<UserSendMsgResp> manageSendMsg(String token, ManagementSendMsgReq req) {
        String param = JSON.toJSONString(req);
        Request request = IMApiFactory.buildLoginRequest(token, param, IMApis.MANAGE_SEND_MSG);
        String data = IMApiFactory.callRequest(request);

        TypeReference<IMRespResult<UserSendMsgResp>> typeReference = new TypeReference<IMRespResult<UserSendMsgResp>>() {
        };

        IMRespResult<UserSendMsgResp> result;
        try {
            result = JSON.parseObject(data, typeReference);
        } catch (Exception e) {
            throw new IMSDKException(IMRespCode.parse(), e.getMessage());
        }
        return result;
    }
}
